package com.arice.activity;

import java.util.List;

import com.arice.beans.DataAnalystInfo;
import com.arice.dao.DataAnalystDao;
import com.arice.dao.ExpenseCodeDao;
import com.arice.dao.IncomeCodeDao;
import com.arice.util.DateHelper;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class DataAnalystLeftActivity extends Activity {
	// 收入数据
	private IncomeCodeDao incomeCodeDao;
	
	//支出数据
	private ExpenseCodeDao expenseCodeDao;
	//本页面的数据，控件
	private Intent intent;
	private int userId;
	private TextView tvdataIncome,tvdataExpense;
	private ListView listView;// ListView
	private MyAdapter adapter;
	
	private ImageView image;
	
	private List<DataAnalystInfo> datalist;
	private DataAnalystDao dataDao;
	private DataAnalystInfo data;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_dataanalystleft);
		intent = getIntent();
		userId = intent.getIntExtra("userId", 0);
		//初始化控件
		tvdataIncome = (TextView) findViewById(R.id.tvdataIncome);
		tvdataExpense = (TextView) findViewById(R.id.tvdataExpense);
		listView = (ListView) findViewById(R.id.lvDataAnalystLeft);
		
		incomeCodeDao = new IncomeCodeDao(this);
		expenseCodeDao = new ExpenseCodeDao(this);
		
		dataDao = new DataAnalystDao(this);
		datalist = dataDao.getAllMoneyByuserId(userId);
		double incomeM = 0.0,expenseM = 0.0,allIncome=0.0,allExpense=0.0;
		String dataM;
		for(int i = 0;i<datalist.size();i++){
			data = datalist.get(i);
			dataM = data.getTime();
			String[] temp = dataM.split("-");
			int typeM = data.getType();
			if(typeM == 1){
				allIncome += data.getMoney();
			}else{
				allExpense +=data.getMoney();
			}
			if(temp[1].equals(DateHelper.getMonthByNow())){
				if(typeM == 1){//收入的
					incomeM +=data.getMoney();
				}else{
					expenseM +=data.getMoney();
				}
			}
		}
		
		tvdataIncome.setText("您当前总收入为"+allIncome+"元\r\n"+DateHelper.getMonthByNow()+"月收入为："+incomeM+"元");
		tvdataExpense.setText("您当前总支出为"+allExpense+"元\r\n"+DateHelper.getMonthByNow()+"月支出为："+expenseM+"元");
		
		
		adapter = new MyAdapter();
		listView.setAdapter(adapter);
	}

	// 自定义一个适配器(把数据装到ListView的工具)
	private class MyAdapter extends BaseAdapter {

		public int getCount() { // 获取条目总数
			return datalist.size();
		}

		public Object getItem(int position) { // 根据位置获取对象
			return datalist.get(position);
		}

		public long getItemId(int position) { // 根据位置获取id
			return position;
		}

		// 获取一个条目视图
		public View getView(int position, View convertView, ViewGroup parent) {
			// 重用convertView
			View item = convertView != null ? convertView : View.inflate(
					DataAnalystLeftActivity.this, R.layout.dataitem, null);

			// 获取该视图中的TextView
			TextView tvDataIncometime = (TextView) item.findViewById(R.id.tvDataIncometime);
			TextView tvDataIncomeTypeAndMoney = (TextView) item.findViewById(R.id.tvDataIncomeTypeAndMoney);

			TextView tvDataExpensetime = (TextView) item.findViewById(R.id.tvDataExpensetime);
			TextView tvDataExpenseTypeAndMoney = (TextView) item.findViewById(R.id.tvDataExpenseTypeAndMoney);
			
			image = (ImageView) item.findViewById(R.id.imagedataItem);
			// 根据当前位置获取IncomeInfo对象
			data = datalist.get(position);
			int dataType = data.getType();
			//1为收入income  2为支出expense
			String time = data.getTime();
			double money = data.getMoney();
			if(dataType == 1){
				String codeName = incomeCodeDao.getCodeNameByCodeId(data.getCodeId());
				tvDataIncometime.setText(time);
				tvDataIncomeTypeAndMoney.setText(codeName+":"+money);
				tvDataExpensetime.setText("");
				tvDataExpenseTypeAndMoney.setText("");
				image.setBackgroundResource(R.drawable.income_icon);
			}else if(dataType == 2){
				String codeName = expenseCodeDao.getCodeNameByCodeId(data.getCodeId());
				tvDataIncometime.setText("");
				tvDataIncomeTypeAndMoney.setText("");
				tvDataExpensetime.setText(time);
				tvDataExpenseTypeAndMoney.setText(codeName+":"+money);
				image.setBackgroundResource(R.drawable.expense_icon);
			}

			return item;
		}
	}

}
